<?php
class Nkdefault extends CI_Model
    {
        public function __construct() {
            parent::__construct();
            $this->load->database();
            $this->load->helper('url');
        }

         public function createRandomStr($length = 10)
            {
                $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
                $randomString = '';
                for ($i = 0; $i < $length; $i++) {
                    $randomString .= $characters[rand(0, strlen($characters) - 1)];
                }
                return $randomString;
            }

        public function countRowsResults($table, $where = null)
        {
            if($where != null) $this->db->where($where);
            $query = $this->db->count_all_results($table);
            return $query;
        }
        
        public function countRowsTable($table)
        {
            $query = $this->db->count_all($table);
            return $query;
        }
        
        public function loadDanhMuc($loadAll = false)
        {
            $sql = "";
            //If $loadAll is true, the function will load all categories in database
            //If there is false, the function will load category that has `hienthi` is '1'
            if($loadAll)
                $sql = "SELECT madanhmuc, tendanhmuc FROM danhmuc ORDER BY thutuhienthi";
            else
                $sql = "SELECT madanhmuc, tendanhmuc FROM danhmuc WHERE hienthi = '1' ORDER BY thutuhienthi";
            $query = $this->db->query($sql);
            if($query->num_rows() > 0)
            {                   
                $arrayResult = array();
                foreach($query->result() as $row)
                {   
                    $danhmuccon = $this->loadDanhMucCon($row->madanhmuc, $loadAll);
                    $arrayItem = array("madanhmuc" => $row->madanhmuc, "tendanhmuc"=>$row->tendanhmuc, 'danhmuccon'=> $danhmuccon);                        
                    array_push($arrayResult, $arrayItem);
                }                  
                return $arrayResult;   
            }
            else
                return array();
        }
        
        public function loadDanhMucCon($madanhmuc, $loadAll = false)
        {
            $sql = "";
            //If $loadAll is true, the function will load all categories in database
            //If there is false, the function will load category that has `hienthi` is '1'
            if($loadAll)
                $sql = "SELECT madanhmuc_con, tendanhmuc, hienthi FROM danhmuccon WHERE madanhmuc = '$madanhmuc'";
            else
                $sql = "SELECT madanhmuc_con, tendanhmuc, hienthi FROM danhmuccon WHERE madanhmuc = '$madanhmuc' AND hienthi = '1'";
            $query = $this->db->query($sql);
            if($query->num_rows() > 0)
                return $query->result_array();
            else
                return array();
        }
    }
?>